package com.ruoyi.business.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.business.runner.InitRabbitMQRunner;
import com.ruoyi.business.vo.WxCarDeviceVO;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.WxCarDevice;
import com.ruoyi.business.service.IWxCarDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车牌识别Controller
 * 
 * @author asher
 * @date 2022-05-23
 */
@RestController
@RequestMapping("/business/car_device")
public class WxCarDeviceController extends BaseController
{
    @Autowired
    private IWxCarDeviceService wxCarDeviceService;

    @Autowired
    private InitRabbitMQRunner initRabbitMQRunner;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询车牌识别列表
     */
    @PreAuthorize("@ss.hasPermi('business:car_device:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxCarDevice wxCarDevice)
    {
        startPage();
        List<WxCarDevice> list = wxCarDeviceService.selectWxCarDeviceList(wxCarDevice);
        ArrayList<WxCarDeviceVO> objects = new ArrayList<>();
        list.forEach(item -> {
            WxCarDeviceVO wxCarDeviceVO = new WxCarDeviceVO();
            BeanUtils.copyProperties(item, wxCarDeviceVO);
            String online = redisCache.getCacheObject("CAR_SN_ONLINE_" + item.getSn());
            wxCarDeviceVO.setOnline(StringUtils.isEmpty(online) ? "false" : "true");
            objects.add(wxCarDeviceVO);
        });
        return getDataTable(objects);
    }

    /**
     * 导出车牌识别列表
     */
    @PreAuthorize("@ss.hasPermi('business:car_device:export')")
    @Log(title = "车牌识别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WxCarDevice wxCarDevice)
    {
        List<WxCarDevice> list = wxCarDeviceService.selectWxCarDeviceList(wxCarDevice);
        ExcelUtil<WxCarDevice> util = new ExcelUtil<WxCarDevice>(WxCarDevice.class);
        return util.exportExcel(list, "车牌识别数据");
    }

    /**
     * 获取车牌识别详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:car_device:query')")
    @GetMapping(value = "/{carDeviceId}")
    public AjaxResult getInfo(@PathVariable("carDeviceId") Long carDeviceId)
    {
        return AjaxResult.success(wxCarDeviceService.selectWxCarDeviceByCarDeviceId(carDeviceId));
    }

    /**
     * 新增车牌识别
     */
    @PreAuthorize("@ss.hasPermi('business:car_device:add')")
    @Log(title = "车牌识别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxCarDevice wxCarDevice) throws IOException {
        int i = wxCarDeviceService.insertWxCarDevice(wxCarDevice);
        initRabbitMQRunner.initNewDevice(wxCarDevice);
        return toAjax(i);
    }

    /**
     * 修改车牌识别
     */
    @PreAuthorize("@ss.hasPermi('business:car_device:edit')")
    @Log(title = "车牌识别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxCarDevice wxCarDevice)
    {
        return toAjax(wxCarDeviceService.updateWxCarDevice(wxCarDevice));
    }

    /**
     * 删除车牌识别
     */
    @PreAuthorize("@ss.hasPermi('business:car_device:remove')")
    @Log(title = "车牌识别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carDeviceIds}")
    public AjaxResult remove(@PathVariable Long[] carDeviceIds)
    {
        return toAjax(wxCarDeviceService.deleteWxCarDeviceByCarDeviceIds(carDeviceIds));
    }
}
